package com.ytf.test.third;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author: YTF
 * @date: 2024/9/12 11:52
 * @version: 1.0
 * @description: 等价比较
 */
public class Equivalence {
    public static void main(String[] args) {
        System.out.println("Integer 数字小于128");
        Integer i1 = new Integer(47);
        Integer i2 = new Integer(47);
        System.out.println(i1 == i2);
        System.out.println(i1 != i2);
        System.out.println("Integer 数字大于等于128");
        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println(i3 == i4);
        System.out.println(i3 != i4);
        System.out.println(i3.equals(i4));
        System.out.println(!i3.equals(i4));
        System.out.println("char 数字大于等于128");
        Character c1 = 128;
        Character c2 = 128;
        System.out.println(c1 == c2);
        System.out.println(c1 != c2);
        System.out.println(c1.equals(c2));
        System.out.println(!c1.equals(c2));
        System.out.println("Long 数字大于等于128");
        Long l1 = 128L;
        Long l2 = 128L;
        System.out.println(l1 == l2);
        System.out.println(l1 != l2);
        System.out.println(l1.equals(l2));
        System.out.println(!l1.equals(l2));
        System.out.println("Double 类型");
        Double d1 = 128.8D;
        Double d2 = 128.8D;
        System.out.println(d1 == d2);
        System.out.println(d1 != d2);
        System.out.println(d1.equals(d2));
        System.out.println(!d1.equals(d2));
        System.out.println("BigDecimal 用例");
        BigDecimal bigDecimal1 = BigDecimal.valueOf(128.8);
        BigDecimal bigDecimal2 = BigDecimal.valueOf(128.8);
        System.out.println(bigDecimal1.compareTo(bigDecimal2));
        /**
         * output:
         * false
         * true
         * false
         * true
         * true
         * false
         * 总结：
         *  就算是基本类型在比较时也需要注意比较方式，
         *  整数如果超出范围需要用 equals 比较 小数必须equals
         *  根据情况使用 BigInteger 和 BigDecimal 这俩非常重要 0相等 -1小于 1大于
         */

        Value value1 = new Value();
        Value value2 = new Value();
        value1.i = value2.i = 100;
        System.out.println(value1.equals(value2));
        /**
         * output
         * false 注意实体比较需要重写equals方法
         */
    }
}
class Value{
    int i;
}
